package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.PetDto;
import cn.itsource.pet.domain.PetType;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.query.PetQuery;
import cn.itsource.pet.service.IPetService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//Propagation.SUPPORTS:支持别人事务
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class PetServiceImpl extends BaseServiceImpl<Pet, PetQuery> implements IPetService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private ShopMapper shopMapper;


    @Override
    public PageList<Pet> findPage(PetQuery petQuery) {

        PageHelper.startPage(petQuery.getPage(), petQuery.getSize());

        List<Pet> pets = petMapper.findPage(petQuery);

        PageInfo<Pet> pageInfo = new PageInfo<>(pets);

        return new PageList<>(pageInfo.getTotal(), pageInfo.getList());

    }

    @Override
    @Transactional
    public AjaxResult upOrDownPet(PetDto petDto) {

        Long state = petDto.getState();
        Long[] ids = petDto.getIds();

        for (Long id : ids) {

            Pet pet = new Pet();
            pet.setId(id);
            pet.setState(state);


            petMapper.updateByPrimaryKeySelective(pet);
        }

        return AjaxResult.createSuccess();
    }

    @Override
    public PageList<Pet> queryHomePage(PetDto petDto) {

        PageHelper.startPage(petDto.getPage(), petDto.getSize());
        List<Pet> pets = petMapper.queryHomePage(petDto);

        PageInfo<Pet> pageInfo = new PageInfo<>(pets);

        return new PageList<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void saveOrUpdate(Pet pet) {
        if (pet.getId() == null) {


            //1.维护店铺id
            Long shopId = GetUserInfoUtil.getShopId();
            pet.setShopId(shopId);
            petMapper.insertSelective(pet);

            //2.入库详情表
            PetDetail petDetail = new PetDetail();
            petDetail.setAdoptNotice(pet.getAdoptNotice());
            petDetail.setIntro(pet.getIntro());
            petDetail.setPetId(pet.getId());
            petDetailMapper.insertSelective(petDetail);

        } else {
            //1.修改
            petMapper.updateByPrimaryKeySelective(pet);

            //2.入库详情表
            PetDetail petDetail = new PetDetail();
            petDetail.setAdoptNotice(pet.getAdoptNotice());
            petDetail.setIntro(pet.getIntro());
            petDetail.setPetId(pet.getId());
            petDetailMapper.insertSelective(petDetail);
        }

    }

    @Override
    public AjaxResult detail(Long id) {
        Pet pet = petMapper.selectByPrimaryKey(id);

        Shop shop = shopMapper.selectByPrimaryKey(pet.getShopId());
        pet.setShopName(shop.getName());

        PetDetail petDetail = petDetailMapper.selectByPetId(pet.getId());

        pet.setAdoptNotice(petDetail.getAdoptNotice());
        pet.setIntro(petDetail.getIntro());
        return AjaxResult.createSuccess(pet);
    }
}
